package com.bw.parkingspace_module.parking

import androidx.lifecycle.viewModelScope
import com.bw.parkingspace_module.parking.entity.ParkingValueEntity
import com.kf.common.entity.BaseEntity
import com.kf.common.mvi.viewmodel.BaseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class ParkingViewModel : BaseViewModel<ParkingModel, ParkingState>() {

    val list: ArrayList<ParkingValueEntity> = ArrayList()

    override fun createState(): ParkingState {
        return ParkingState(BaseEntity(0, "", list))
    }

    override fun createModel(): ParkingModel {
        return ParkingModel()
    }

    fun requestParkingAll() {
        viewModelScope.launch(Dispatchers.IO) {
            mModel.requestParkingAll()
                .flowOn(Dispatchers.IO)
                .collect {
                    mutableContainer.updateUiState { copy(entity = it) }
                }
        }
    }

}